<?php

namespace Module;

class PostModule extends BaseModule {

    public function indexAction() {
        $args = \Lib\Registry::get('args'); 
        $postId = (int)$args['Id'];
        $post = \Lib\Model\PostModel::getById($postId);
        if(!$post){
            return $this->pageNotFound();
        }
        \Lib\Model\PostModel::setView($postId);
        $post->tags = \Lib\Model\PostModel::getTags($postId);
        $post->views = \Lib\Model\PostModel::getView($postId);

        if($post->user == \Lib\Model\UserModel::getUser() || \Lib\Model\UserModel::isAdmin()){
            $showInfo = TRUE;
        } else {
            $showInfo = FALSE;
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'post'=> (array)$post,
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                \Lib\Registry::get('view')->__($post->category_en) => '/category/'.$post->category_en,
                $post->title => '/post/'.$postId
             ),
            'title'=> $post->title,
            'showInfo' => $showInfo
            ));
    }
    
    public function editAction() {
        if(!\Lib\Model\UserModel::isAuth()){
            return $this->pageNotFound();
        }
        $args = \Lib\Registry::get('args'); 
        $postId = (int)$args['Id'];
        $post = \Lib\Model\PostModel::getById($postId);
        if(!$post){
            return $this->pageNotFound();
        }
        if($post->user != \Lib\Model\UserModel::getUser() && !\Lib\Model\UserModel::isAdmin()){
            return $this->pageNotFound();
        }
        
        $tags = \Lib\Model\PostModel::getTags($postId);
        foreach ($tags as &$tag) {
            $tag = $tag['tag']; 
        }
        $post->tags = implode(', ', $tags);
        
        $categories = \Lib\Model\CategoryModel::getAll();
        
        $error = '';
        
        if(isset($_POST['text']) && isset($_POST['text'])){
            $cat = $_POST['category'];
            $title = $_POST['title'];
            $text = $_POST['text'];
            $tags = explode(',', $_POST['tags']);
            $description = $_POST['description'];
            foreach ($tags as &$tag) {
                $tag = strip_tags(trim($tag));
                if(empty($tag)){
                    unset ($tag);
                }
            }
            \Lib\Model\PostModel::setTags($postId, $tags);
            
            if(mb_strlen($title) < 255 && mb_strlen($text) < 64000 && mb_strlen($description) < 2000 &&  $postId = \Lib\Model\PostModel::update($postId, $cat, $title, $text, $description)){
                header( 'Location: /post/'.$postId, true, 301 );
            } else {
                    $error = 'Internal error';
            }
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'post'=> (array)$post,
            'categories' => $categories,
            'error' => $error,
            'title'=> $post->title
            ));
    }
    
        public function deleteAction() {
        if(!$user = \Lib\Model\UserModel::getUser()){
            return $this->pageNotFound();
        }    
        $args = \Lib\Registry::get('args'); 
        $postId = (int)$args['Id'];
        $post = \Lib\Model\PostModel::getById($postId);
        if(!$post){
            return $this->pageNotFound();
        }
        if($post->user != $user && !\Lib\Model\UserModel::isAdmin()){
            return $this->pageNotFound();
        }
        
        \Lib\Model\PostModel::update($postId, $post->category_en, $post->title, $post->post, $post->description, 4);

        return $this->showTemplate(__FUNCTION__, array(
            'title'=> \Lib\Registry::get('view')->__('Post deleted')
            ));
    }
    
    public function publishAction() {
        $args = \Lib\Registry::get('args'); 
        $postId = (int)$args['Id'];
        $post = \Lib\Model\PostModel::getById($postId);
        if(!$post){
            return $this->pageNotFound();
        }

        return $this->showTemplate(__FUNCTION__, array(
            'post'=> (array)$post,
            'title'=> $post->title
            ));
    }
    
    public function categoryAction() {
        $args = \Lib\Registry::get('args'); 
        $categoryName = $args['category'];
        $category = \Lib\Model\CategoryModel::getByName($categoryName);
        if(!$category){
            return $this->pageNotFound();
        }
        $posts = \Lib\Model\PostModel::getByCategoryId($category["id"]);
        foreach ($posts as &$post) {
            if ($post->user == \Lib\Model\UserModel::getUser() || \Lib\Model\UserModel::isAdmin()) {
                $post->showInfo = TRUE;
            } else {
                $post->showInfo = FALSE;
            }
            $post->tags = \Lib\Model\PostModel::getTags($post->id);
            $post->views = \Lib\Model\PostModel::getView($post->id);
        }
        return $this->showTemplate(__FUNCTION__, array(
            'title'=> $category["name_".\Lib\Registry::get('trans')->getLocale()],
            'posts' => $posts,
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                $category["name_".\Lib\Registry::get('trans')->getLocale()] => '/category/'.$categoryName
            ),
            'category' => $category
                ));
    }
    
    public function topPostsMenuAction() {
        $posts = \Lib\Model\PostModel::getTop();
        return $this->showTemplate(__FUNCTION__, array(
            'posts' => $posts,
            ),'');
    }
    
    public function tagsMenuAction() {
        $tags = \Lib\Model\PostModel::getTagsForMenu();
        return $this->showTemplate(__FUNCTION__, array(
            'tags' => $tags,
            ),'');
    }
    
    public function tagsAction() {
        $tags = \Lib\Model\PostModel::getAllTags();
        
        return $this->showTemplate(__FUNCTION__, array(
            'tags' => $tags,
            'title'=> \Lib\Registry::get('view')->__('Tags')
            ));
    }
    
    public function topPostsAction() {
        $posts = \Lib\Model\PostModel::getTop();

        foreach ($posts as &$post) {
            if ($post->user == \Lib\Model\UserModel::getUser() || \Lib\Model\UserModel::isAdmin()) {
                $post->showInfo = TRUE;
            } else {
                $post->showInfo = FALSE;
            }
            $post->tags = \Lib\Model\PostModel::getTags($post->id);
            $post->views = \Lib\Model\PostModel::getView($post->id);
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                \Lib\Registry::get('view')->__('Top posts') => '/post/topPosts'
            ),
            'posts' => $posts,
            'title'=> \Lib\Registry::get('view')->__('Top posts')
            ));
    }
    
    public function latestPostsAction() {
        $posts = \Lib\Model\PostModel::getLatest();

        foreach ($posts as &$post) {
            if ($post->user == \Lib\Model\UserModel::getUser() || \Lib\Model\UserModel::isAdmin()) {
                $post->showInfo = TRUE;
            } else {
                $post->showInfo = FALSE;
            }
            $post->tags = \Lib\Model\PostModel::getTags($post->id);
            $post->views = \Lib\Model\PostModel::getView($post->id);
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'posts' => $posts,
            'title'=> \Lib\Registry::get('view')->__('Latest posts')
            ));
    }
    
    public function tagAction() {
        $args = \Lib\Registry::get('args'); 
        $tag = trim($args['tag']);
        $posts = \Lib\Model\PostModel::getByTag($tag);

        foreach ($posts as &$post) {
            if ($post->user == \Lib\Model\UserModel::getUser() || \Lib\Model\UserModel::isAdmin()) {
                $post->showInfo = TRUE;
            } else {
                $post->showInfo = FALSE;
            }
            $post->tags = \Lib\Model\PostModel::getTags($post->id);
            $post->views = \Lib\Model\PostModel::getView($post->id);
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'posts' => $posts,
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                \Lib\Registry::get('view')->__('Posts by tag').' "'.$tag.'"' => '/tag/'.$tag
            ),
            'tag' => $tag,
            'title'=> \Lib\Registry::get('view')->__('Posts by tag').' "'.$tag.'"'
            ));
    }
    
    public function addAction() {
        if(!\Lib\Model\UserModel::isAuth()){
            header( 'Location: /', true, 301 );
        }
        
        $categories = \Lib\Model\CategoryModel::getAll();

        $error = '';
        
        if(isset($_POST['text']) && isset($_POST['text'])){
            $cat = $_POST['category'];
            $title = $_POST['title'];
            $text = $_POST['text'];
            $tags = explode(',', $_POST['tags']);
            $description = $_POST['description'];
            foreach ($tags as &$tag) {
                $tag = strip_tags(trim($tag));
                if(empty($tag)){
                    unset ($tag);
                }
            }
            
            if(mb_strlen($title) < 255 && mb_strlen($text) < 64000 && mb_strlen($description) < 2000 &&   $postId = \Lib\Model\PostModel::add($cat, $title, $text, $description)){
                \Lib\Model\PostModel::setTags($postId, $tags);
                header( 'Location: /post/'.$postId, true, 301 );
            } else {
                $error = 'Internal error';
            }
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'categories' => $categories,
            'error'=> $error,
            'title'=> \Lib\Registry::get('view')->__('Add post')
            ));
    }
    
    public function listByUserAction() {
        $args = \Lib\Registry::get('args'); 
        $userName = $args['user'];
        $posts = \Lib\Model\PostModel::getByUserId($userName);
        foreach ($posts as &$post) {
            if ($post->user == \Lib\Model\UserModel::getUser() || \Lib\Model\UserModel::isAdmin()) {
                $post->showInfo = TRUE;
            } else {
                $post->showInfo = FALSE;
            }
            $post->tags = \Lib\Model\PostModel::getTags($post->id);
            $post->views = \Lib\Model\PostModel::getView($post->id);
        }
        return $this->showTemplate(__FUNCTION__, array(
            'title'=> \Lib\Registry::get('view')->__('Post list of user').' '.\Lib\Model\UserModel::getUser(),
            'posts' => $posts,
            ));
    }

}

?>
